package com.cdks.wuxiu.api.service.mysql;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.mapper.VoiceShowInfoMapper;
import com.cdks.wuxiu.entity.mysql.VoiceShowInfo;

import java.util.List;

import org.springframework.stereotype.Service;

/**
 * 用户语音 服务实现类
 */
@Service
public class VoiceShowInfoServiceImpl extends ServiceImpl<VoiceShowInfoMapper, VoiceShowInfo> implements VoiceShowInfoService {
    @Override
    public int deleteByUser(Long userId) {
        QueryWrapper<VoiceShowInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(VoiceShowInfo::getUserId, userId);
        baseMapper.delete(wrapper);
        
        return 0;
    }

    @Override
    public int deleteByName(Long userId, String videoUri) {
        QueryWrapper<VoiceShowInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(VoiceShowInfo::getVoiceUri, videoUri)
                        .eq(VoiceShowInfo::getUserId, userId);
        baseMapper.delete(wrapper);
        
        return 0;
    }

    public List<VoiceShowInfo> getVoicesByUser(Long userId) {
        QueryWrapper<VoiceShowInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(VoiceShowInfo::getUserId, userId)
                        .eq(VoiceShowInfo::getStatus, Constant.AUDIT_STATUS_PASS)
                        .select(VoiceShowInfo::getId,
                                VoiceShowInfo::getVoiceUri,
                                VoiceShowInfo::getPlayCnt,
                                VoiceShowInfo::getRewardCnt)
                        .orderByDesc(VoiceShowInfo::getId);

        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<VoiceShowInfo> getPlayList(long selfId, int sex) {
        if (selfId == 0)
        {
            QueryWrapper<VoiceShowInfo> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(VoiceShowInfo::getStatus, Constant.AUDIT_STATUS_PASS)
                            .select(VoiceShowInfo::getId,
                                    VoiceShowInfo::getUserId,
                                    VoiceShowInfo::getVoiceUri,
                                    VoiceShowInfo::getPlayCnt,
                                    VoiceShowInfo::getRewardCnt)
                            .orderByDesc(VoiceShowInfo::getId);

            return baseMapper.selectList(wrapper.last("LIMIT 5"));
        }

        List<VoiceShowInfo> res  = baseMapper.getPlayList(selfId, sex);
        if (res.isEmpty())
        {
            return baseMapper.getRandList(selfId, sex);
        }

        return res;
    }

    @Override
    public int addPlayCount(Long voiceId) {
        return baseMapper.addPlayCount(voiceId);
    }

    @Override
    public int addRewardCount(Long voiceId) {
        return baseMapper.addRewardCount(voiceId);
    }    
}
